package pers.fulsun.blog.service.impl;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import java.sql.Timestamp;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;
import pers.fulsun.blog.dao.ArticleDao;
import pers.fulsun.blog.dao.es.ArticleEsDao;
import pers.fulsun.blog.entity.Article;
import pers.fulsun.blog.entity.EsArticle;
import pers.fulsun.blog.service.ArticleService;

/**
 * 服务接口实现
 *
 * @author fl
 * @since 2021-11-29 11:19:42
 * @description 由 Mybatisplus Code Generator 创建
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class ArticleServiceImpl implements ArticleService {
  private final ArticleDao articleDao;

  @Autowired private ElasticsearchRestTemplate elasticsearchRestTemplate;
  @Autowired private ArticleEsDao articleEsDao;

  @Override
  public List<EsArticle> getArticleByState(
      Integer state, Integer currentPage, Integer count, String keywords) {
    NativeSearchQuery nativeSearchQuery =
        new NativeSearchQueryBuilder()
            .withQuery(QueryBuilders.matchQuery("mdContent", keywords))
            .withPageable(PageRequest.of(currentPage - 1, count))
            .build();
    System.out.println(nativeSearchQuery.getQuery().toString());
    SearchHits<EsArticle> searchHits =
        elasticsearchRestTemplate.search(nativeSearchQuery, EsArticle.class);
    List<EsArticle> searchArticleList =
        searchHits.stream().map(SearchHit::getContent).collect(Collectors.toList());
    return searchArticleList;
    //    IPage<Article> articlePage =
    //        articleDao.page(
    //            new Page(currentPage, count),
    //            new QueryWrapper<Article>()
    //                .lambda()
    //                .eq(Article::getIsdelete, 0)
    //                .like(!StringUtils.isEmpty(keywords), Article::getTitle, keywords));
    //    return articlePage;
  }

  @Override
  public boolean addNewArticle(Article article) throws JsonProcessingException {
    if (article.getIsdraft() == 1) { // 发布
      Timestamp timestamp = new Timestamp(System.currentTimeMillis());
      // 添加操作
      if (article.getId() == -1) {
        article.setId(null);
        article.setCreatetime(timestamp);
      } else {
        // 更新
      }
      article.setUpdatetime(timestamp);
      // todo 打标签
      String[] dynamicTags = article.getDynamicTags();
      if (dynamicTags != null && dynamicTags.length > 0) {}
    }
    article.setUserid(6);
    articleDao.saveOrUpdate(article);
    //    elasticsearchRestTemplate.delete(article);
    EsArticle esArticle = JSON.parseObject(JSON.toJSONString(article), EsArticle.class);
    articleEsDao.save(esArticle);
    //    elasticsearchRestTemplate.save(product);
    return true;
  }

  @Override
  public Article getArticleById(Long aid) {
    return articleDao.getById(aid);
  }
}
